package com.qust.edu.aliyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qust.edu.aliyun.mapper.CategoryMapper;
import com.qust.edu.aliyun.pojo.Category;
import com.qust.edu.aliyun.service.CategoryService;
import com.qust.edu.aliyun.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: aliyun_guanli
 *
 * @author: 姜龙
 * time: 2021/9/28 17:16
 * description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有分类
     * @return
     */

    @Override
    public Object getCategory() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",0 );


        List<Category> categories = categoryMapper.selectList(queryWrapper);


        Map data = new HashMap<>();
        data.put("code","001");
        data.put("Category",categories);

        return data;
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @Override
    public R getAddCategory(Category category) {
        int rows=0;
        category.setCategoryId(null);
        category.setDeleted(0);

        try {
            rows=categoryMapper.insert(category);
        }catch (Exception e){
            System.out.println("e = " + e);
        }
        if (rows == 0){
            return R.FAIL("保存失败！");
        }
//        3.处理结果
        return R.OK("保存成功！");
    }


    /**
     * 删除分类
     * @param categoryId
     * @return
     */
    @Override
    public Object getDeleteCategory(Integer categoryId) {
        Category category = new Category();
        category.setCategoryId(categoryId);
        category.setDeleted(1);

        int rows=categoryMapper.updateById(category);

        if (rows==0){
//            Map data = new HashMap<>();
//            data.put("code","004");
//            data.put("删除分类失败",rows);

            return "删除分类失败";

        }
//        Map data = new HashMap<>();
//        data.put("code","001");
//        data.put("Category",rows);

        return R.OK("删除分类成功");
    }
}
